<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

ini_set ( 'display_errors', 'On' );
Error_reporting ( E_ALL );
class api extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->helper ( "common" );
	}
	public function material($param) {
		switch ($param) {
			case "region" :
				$this->material_region ();
				break;
			case "supplier" :
				$this->material_supplier ();
				break;
			case "media_type" :
				$this->material_media_type ();
				break;
			case "craftsmanship" :
				$this->material_craftsmanship ();
				break;
			case "corner" :
				$this->material_corner ();
				break;
			case "media_area" :
				$this->material_media_area ();
				break;
			case "traffic_flow" :
				$this->material_traffic_flow ();
				break;
			case "highway" :
				$this->material_highway ();
				break;
			case "highway_section" :
				$this->material_highway_section ();
				break;
			case "checkpoint" :
				$this->material_highway_point ();
				break;
			case "airport" :
				$this->material_airport ();
				break;
			case "business_area" :
				$this->material_business_area ();
				break;
			case "main_road" :
				$this->material_main_road ();
				break;
			case "market" :
				$this->material_market ();
				break;
			case "railway_station" :
				$this->material_railway_station ();
				break;
			case "bus_station" :
				$this->material_bus_station ();
				break;
			case "major_venues" :
				$this->material_major_venues ();
				break;
			case "brand" :
				$this->material_brand ();
				break;
			case "industry" :
				$this->material_industry ();
				break;
			case "resolution" :
				$this->material_resolution ();
				break;
			case "agreement" :
				$this->material_agreement ();
				break;
			case "play_format" :
				$this->material_play_format ();
				break;
		}
	}
	public function account($param) {
		switch ($param) {
			case "login" :
				$this->account_login ();
				break;
			case "get_profile" :
				$this->account_get_profile ();
				break;
			case "logout" :
				$this->account_logout ();
				break;
		}
	}
	public function media($param) {
		switch ($param) {
			case "get_list" :
				$this->media_get_list ();
				break;
			case "get_content" :
				$this->media_get_content ();
				break;
			case "search" :
				$this->media_search ();
				break;
			case "submit_draft" :
				$this->media_submit_draft ();
				break;
			case "upload_pic" :
				$this->media_upload_pic ();
				break;
			case "delete_audit" :
				$this->media_delete_audit ();
				break;
			case "submit_audit" :
				$this->media_submit_audit ();
				break;
			case "nearby_media" :
				$this->media_nearby_media ();
				break;
			case "delete_draft_pic" :
				$this->media_delete_draft_pic ();
				break;
		}
	}
	public function supervise($param) {
		switch ($param) {
			case "submit_monitor" :
				$this->supervise_submit_monitor ();
				break;
			case "delete_monitor" :
				$this->supervise_delete_monitor ();
				break;
			case "update_monitor" :
				$this->supervise_update_monitor ();
				break;
			case "get_monitor" :
				$this->supervise_get_monitor ();
				break;
			case "get_list" :
				$this->supervise_get_list ();
				break;
			case "upload_pic" :
				$this->supervise_upload_pic ();
				break;
		}
	}
	public function account_login() {
		$user_name = trim ( $this->input->get_post ( "username" ) );
		$password = trim ( $this->input->get_post ( "password" ) );
		
		$this->load->library ( "common/login_manage" );
		$result = $this->login_manage->mobile_login ( $user_name, $password );
		echo JSON ( $result );
	}
	private function get_user_id() {
		$token = $this->input->get_post ( "token" );
		if ($token == "") {
			$result ['ret_code'] = 3;
			$result ['error_code'] = '300';
			$result ['msg'] = "token 无效";
			echo JSON ( $result );
			die ();
		}
		$this->load->library ( "common/login_manage" );
		$result = $this->login_manage->mobile_query_user_id ( $token );
		if ($result == null) {
			$result ['ret_code'] = 3;
			$result ['error_code'] = '300';
			$result ['msg'] = "token 无效";
			echo JSON ( $result );
			die ();
		} else {
			return $result ['user_id'];
		}
	}
	public function account_logout() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$this->load->library ( "common/login_manage" );
		$this->login_manage->mobile_logout ( $this->get_user_id () );
		echo JSON ( $result );
	}
	public function account_get_profile() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$user_id = $this->get_user_id ();
		$this->load->library ( "common/user_manage" );
		$user_detail = $this->user_manage->query_user_detail ( $user_id );
		if ($user_detail == null) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 101;
			$result ['msg'] = "用户信息不存在";
			$result ['data'] = null;
			echo JSON ( $result );
		} else {
			$result ['data'] = array (
					"username" => $user_detail ['user_name'],
					"name" => $user_detail ['name'],
					"gender" => $user_detail ['sex'],
					"mail" => $user_detail ['email'],
					"mobile" => $user_detail ['mobile'] 
			);
			echo JSON ( $result );
		}
	}
	public function media_get_list() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$this->load->library ( "media/media" );
		$user_id = $this->get_user_id ();
		$this->load->library ( "common/user_manage" );
		$user_detail = $this->user_manage->query_user_detail ( $user_id );
		$sort = $this->input->get ( "sort" );
		$media_list = $this->media->mobile_query_my_media_draft_list ( $user_detail ['user_name'], $user_id, $sort );
		$result ['data'] ['media_list'] = array ();
		if ($media_list) {
			foreach ( $media_list as $media ) {
				$status = "";
				$pre = "";
				if ($media ['media_status'] === null) {
					$status = "AUDIT_PASS";
				}
				if ($media ['media_status'] === "0") {
					$pre = "D_";
					$status = "DRAFT";
				}
				if ($media ['media_status'] == "10") {
					$pre = "D_";
					$status = "TO_AUDIT";
				}
				if ($media ['media_status'] == "20") {
					$pre = "D_";
					$status = "AUDIT_NOT_PASS";
				}
				
				$result ['data'] ['media_list'] [] = array (
						"id" => $pre . $media ['media_id'],
						"class" => $media ['media_type'],
						"status" => $status,
						"location" => $media ['location'],
						"region" => array (
								array (
										"id" => $media ['province'],
										"name" => $media ['province_name'] 
								),
								array (
										"id" => $media ['city'],
										"name" => $media ['city_name'] 
								),
								array (
										"id" => $media ['region'],
										"name" => $media ['region_name'] 
								) 
						) 
				);
			}
		}
		echo JSON ( $result );
	}
	public function media_search() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$this->load->library ( "media/media" );
		$user_id = $this->get_user_id ();
		$this->load->library ( "common/user_manage" );
		$user_detail = $this->user_manage->query_user_detail ( $user_id );
		$keyword = urldecode ( $this->input->get ( "keyword" ) );
		$sort = $this->input->get ( "sort" );
		
		$media_list = $this->media->mobile_search_my_media_draft_list ( $user_detail ['user_name'], $user_id, $sort, $keyword );
		$result ['data'] ['media_list'] = array ();
		if ($media_list) {
			foreach ( $media_list as $media ) {
				$status = "";
				$pre = "";
				if ($media ['media_status'] === null) {
					
					$status = "AUDIT_PASS";
				}
				if ($media ['media_status'] === "0") {
					$pre = "D_";
					$status = "DRAFT";
				}
				if ($media ['media_status'] == "10") {
					$pre = "D_";
					$status = "TO_AUDIT";
				}
				if ($media ['media_status'] == "20") {
					$pre = "D_";
					$status = "AUDIT_NOT_PASS";
				}
				
				$result ['data'] ['media_list'] [] = array (
						"id" => $pre . $media ['media_id'],
						"class" => $media ['media_type'],
						"status" => $status,
						"location" => $media ['location'],
						"region" => array (
								array (
										"id" => $media ['province'],
										"name" => $media ['province_name'] 
								),
								array (
										"id" => $media ['city'],
										"name" => $media ['city_name'] 
								),
								array (
										"id" => $media ['region'],
										"name" => $media ['region_name'] 
								) 
						) 
				);
			}
		}
		echo JSON ( $result );
	}
	public function media_get_content() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$media_id = $this->input->get ( "media_id" );
		/**
		 * 判断是草稿还是媒体
		 */
		$be_media = true;
		if (substr ( $media_id, 0, 2 ) == 'D_') {
			$be_media = false;
			$media_id = substr ( $media_id, 2 );
		}
		$this->load->library ( "media/media" );
		
		/**
		 * 媒体和草稿分开查询
		 */
		if ($be_media) {
			$media_info = $this->media->query_media_detail ( $media_id );
		} else {
			$media_info = $this->media->query_media_draft_detail ( $media_id );
			$media_info ['media_id'] = $media_info;
		}
		
		if ($media_info == null) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 101;
			$result ['msg'] = "媒体信息不存在";
			$result ['data'] = null;
			echo JSON ( $result );
		} else {
			
			/**
			 * 媒体和草稿分开查询
			 */
			if ($be_media) {
				$media_pic_list = $this->media->query_media_pic_list ( $media_id );
			} else {
				$media_pic_list = $this->media->query_media_draft_pic_list ( $media_id );
			}
			
			$pics = array ();
			foreach ( $media_pic_list as $media_pic ) {
				$thumb = $media_pic ['pic_thumb'] == "" ? "" : base_url () . "acc_files_path" . $media_pic ['pic_thumb'];
				$pic = $media_pic ['pic'] == "" ? "" : base_url () . "acc_files_path" . $media_pic ['pic'];
				$pic_id = $media_pic ['pic_id'] == "" ? "" : $media_pic ['pic_id'];
				$pics [] = array (
						$thumb,
						$pic,
						$pic_id 
				);
			}
			
			$status = "";
			$pre = "";
			if ($media_info ['media_status'] === null) {
				$status = "AUDIT_PASS";
			}
			if ($media_info ['media_status'] === "0") {
				$pre = "D_";
				$status = "DRAFT";
			}
			if ($media_info ['media_status'] == "10") {
				$pre = "D_";
				$status = "TO_AUDIT";
			}
			if ($media_info ['media_status'] == "20") {
				$pre = "D_";
				$status = "AUDIT_NOT_PASS";
			}
			
			$airport = array (
					"id" => $media_info ['airport_id'],
					"name" => "" 
			);
			$highway = array (
					array (
							"id" => $media_info ['highway_id'],
							"name" => "" 
					),
					array (
							"id" => $media_info ['section_id'],
							"name" => "" 
					),
					array (
							"id" => $media_info ['point_id'],
							"name" => "" 
					) 
			);
			$urban = array (
					array (
							"id" => "",
							"name" => "" 
					), // 商圈
					array (
							"id" => "",
							"name" => "" 
					), // 主干道
					array (
							"id" => "",
							"name" => "" 
					), // 专业市场
					array (
							"id" => "",
							"name" => "" 
					), // 火车站
					array (
							"id" => "",
							"name" => "" 
					), // 汽车站
					array (
							"id" => "",
							"name" => "" 
					), // 大型场馆
					array (
							"id" => "",
							"name" => "" 
					), // CBD
					array (
							"id" => "",
							"name" => "" 
					), // 政务区
					   // 工业区
					array (
							"id" => "",
							"name" => "" 
					) 
			);
			if ($media_info ['airport_id'] != "") {
				$this->load->library ( "env/env_airport" );
				$env_airport_info = $this->env_airport->query_env_airport ( $media_info ['airport_id'] );
				$airport = array (
						"id" => $media_info ['airport_id'],
						"name" => $env_airport_info ['airport_name'] 
				);
			}
			if ($media_info ['media_type'] == "highway") {
				$this->load->library ( "env/env_highway" );
				$env_highway_info = $this->env_highway->query_env_highway ( $media_info ['highway_id'] );
				$highway [0] = array (
						"id" => $media_info ['highway_id'],
						"name" => $env_highway_info ['highway_name'] 
				);
				
				$this->load->library ( "env/highway_section" );
				$highway_section_info = $this->highway_section->query_highway_section_info ( $media_info ['section_id'] );
				
				$highway [1] = array (
						"id" => $media_info ['section_id'],
						"name" => $highway_section_info ['section_name'] 
				);
				
				$this->load->library ( "env/highway_point" );
				$env_highway_point_info = $this->highway_point->query_highway_point_info ( $media_info ['point_id'] );
				$highway [2] = array (
						"id" => $media_info ['point_id'],
						"name" => $env_highway_point_info ['point_name'] 
				);
			}
			/**
			 * 媒体和草稿分开查询
			 */
			if ($be_media) {
				$env_list = $this->media->query_media_env_list ( $media_id );
			} else {
				$env_list = $this->media->query_media_draft_env_list ( $media_id );
			}
			
			if ($env_list) {
				foreach ( $env_list as $env ) {
					if ($env ['env_type'] == "circle") {
						$this->load->library ( "env/urban_circle" );
						$env_info = $this->urban_circle->query_env_urban_circle ( $env ['env_id'] );
						$urban [0] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					
					if ($env ['env_type'] == "road") {
						$this->load->library ( "env/urban_road" );
						$env_info = $this->urban_road->query_env_urban_road ( $env ['env_id'] );
						$urban [1] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					if ($env ['env_type'] == "market") {
						$this->load->library ( "env/urban_market" );
						$env_info = $this->urban_market->query_env_urban_market ( $env ['env_id'] );
						$urban [2] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					if ($env ['env_type'] == "station") {
						$this->load->library ( "env/urban_station" );
						$env_info = $this->urban_station->query_env_urban_station ( $env ['env_id'] );
						$urban [3] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					if ($env ['env_type'] == "stop") {
						$this->load->library ( "env/urban_stop" );
						$env_info = $this->urban_stop->query_env_urban_stop ( $env ['env_id'] );
						$urban [4] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					if ($env ['env_type'] == "venues") {
						$this->load->library ( "env/urban_venues" );
						$env_info = $this->urban_venues->query_env_urban_venues ( $env ['env_id'] );
						$urban [5] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					if ($env ['env_type'] == "cbd") {
						$this->load->library ( "env/urban_cbd" );
						$env_info = $this->urban_cbd->query_env_urban_cbd ( $env ['env_id'] );
						$urban [6] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					if ($env ['env_type'] == "chief_district") {
						$this->load->library ( "env/urban_chief_district" );
						$env_info = $this->urban_chief_district->query_env_urban_chief_district ( $env ['env_id'] );
						$urban [7] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
					if ($env ['env_type'] == "industrial") {
						$this->load->library ( "env/urban_industrial" );
						$env_info = $this->urban_industrial->query_env_urban_industrial ( $env ['env_id'] );
						$urban [8] = array (
								"id" => $env_info ['env_id'],
								"name" => $env_info ['env_name'] 
						);
					}
				}
			}
			
			$flow = array (
					"id" => "",
					"name" => "" 
			);
			if ($media_info ['flow'] == 10) {
				$flow = array (
						"id" => 10,
						"name" => "极大" 
				);
			}
			if ($media_info ['flow'] == 20) {
				$flow = array (
						"id" => 20,
						"name" => "较大" 
				);
			}
			if ($media_info ['flow'] == 30) {
				$flow = array (
						"id" => 30,
						"name" => "一般" 
				);
			}
			if ($media_info ['flow'] == 40) {
				$flow = array (
						"id" => 40,
						"name" => "较小" 
				);
			}
			
			$agreement = array (
					"id" => "",
					"name" => "" 
			);
			if ($media_info ['agreement']) {
				foreach ( $this->config->item ( "agreement" ) as $am ) {
					if ($media_info ['agreement'] == $am ['id']) {
						$agreement = $am;
						break;
					}
				}
			}
			$play_format = array ();
			if ($media_info ['play_format'] != "") {
				$format_array = explode ( ",", $media_info ['play_format'] );
				foreach ( $format_array as $fa ) {
					foreach ( $this->config->item ( "play_format" ) as $pf ) {
						if ($fa == $pf ['id']) {
							$play_format [] = $pf;
							break;
						}
					}
				}
			}
			$result ['data'] = array (
					"id" => $pre . $media_id,
					"class" => $media_info ['media_type'],
					"location" => $media_info ['location'],
					"status" => $status,
					"supplier" => array (
							"id" => $media_info ['vendor'],
							"name" => $media_info ['company_name'] 
					),
					"region" => array (
							array (
									"id" => $media_info ['province'],
									"name" => $media_info ['province_name'] 
							),
							array (
									"id" => $media_info ['city'],
									"name" => $media_info ['city_name'] 
							),
							array (
									"id" => $media_info ['region'],
									"name" => $media_info ['region_name'] 
							) 
					),
					"highway" => $highway,
					"airport" => $airport,
					"urban" => $urban,
					"media_type" => array (
							"id" => $media_info ['media_type_id'],
							"name" => $media_info ['media_type_name'] 
					),
					"craftsmanship" => array (
							"id" => $media_info ['crafts'],
							"name" => $media_info ['crafts_name'] 
					),
					"longitude" => $media_info ['longitude'],
					"latitude" => $media_info ['latitude'],
					"corner" => array (
							"id" => $media_info ['corner_case'],
							"name" => $media_info ['corner_case_name'] 
					),
					"illumination" => $media_info ['illumination'],
					"flow" => $flow,
					"reference_price" => $media_info ['reference_price'],
					"specification" => $media_info ['specification'],
					"unique_id" => $media_info ['unique_id'],
					"sketch_map" => $media_info ['sketch_map'] == "" ? "" : base_url () . "acc_files_path" . $media_info ['sketch_map'],
					"media_area" => array (
							"id" => $media_info ['media_area'],
							"name" => $media_info ['media_area_name'] 
					),
					"agreement" => $agreement,
					"play_time" => $media_info ['play_time'],
					"play_time_remark" => $media_info ['play_time_remark'],
					"play_format" => $play_format,
					"play_frequency" => $media_info ['play_frequency'],
					"play_frequency_remark" => $media_info ['play_frequency_remark'],
					"resolution" => array (
							"id" => $media_info ['resolution'],
							"name" => $media_info ['resolution'] 
					),
					"specification_width" => $media_info ['specification_width'],
					"specification_height" => $media_info ['specification_height'],
					"price_remark" => $media_info ['price_remark'],
					"pics" => $pics 
			);
			echo JSON ( $result );
		}
	}
	public function media_submit_audit() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$user_id = $this->get_user_id ();
		$this->load->library ( "common/user_manage" );
		$user_detail = $this->user_manage->query_user_detail ( $user_id );
		if ($user_id) {
			$this->load->library ( "media/media" );
			$media_draft_id = $this->input->post ( 'id' );
			if (substr ( $media_draft_id, 0, 2 ) == 'D_') {
				$media_draft_id = substr ( $media_draft_id, 2 );
			}
			$this->media->submit_check_draft ( $media_draft_id, $user_detail ['user_name'] );
		}
		echo JSON ( $result );
	}
	public function media_delete_audit() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$this->load->library ( "media/media" );
		$media_id = $this->input->post ( 'id' );
		if (substr ( $media_id, 0, 2 ) == 'D_') {
			$media_id = substr ( $media_id, 2 );
			$this->media->delete_media_draft ( $media_id );
		} else {
			$this->media->delete_media ( $media_id );
		}
		
		echo JSON ( $result );
	}
	public function media_upload_pic() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$this->load->library ( "media/media" );
		$media_id = $this->input->post ( "media_id" );
		
		$be_media = true;
		if (substr ( $media_id, 0, 2 ) == 'D_') {
			$be_media = false;
			$media_id = substr ( $media_id, 2 );
		}
		
		$pic_type = $this->input->post ( "pic_type" );
		$upload_result = $this->media->save_map ( "pic" );
		if ($upload_result ['result'] == false) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 1;
			$result ['msg'] = $upload_result ['message'];
		} else {
			$result ['data'] ['upload_file'] = $upload_result ['upload_file'];
			$this->media->create_media_thumb ( $upload_result ['upload_file'] );
			if ($pic_type == "sketch_map") {
				$media ['sketch_map'] = $result ['data'] ['upload_file'];
				$media ['media_status'] = 0;
				$this->media->update_media_draft_sketch_map ( $media, $media_id );
			} else {
				$this->media->add_media_draft_pic ( $result ['data'] ['upload_file'], $media_id );
				$media ['media_status'] = 0;
				$this->media->update_media_draft_sketch_map ( $media, $media_id );
			}
		}
		echo JSON ( $result );
	}
	public function media_delete_draft_pic() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$user_id = $this->get_user_id ();
		$this->load->library ( "media/media" );
		$pic_id = $this->input->post ( "pic_id" );
		log_message ( "info", "删除的图片是:" . $pic_id );
		if ($pic_id != "") {
			log_message ( "info", "我要去删图片了!" );
			foreach ( $pic_id as $id ) {
				$this->media->delete_draft_pic ( $id );
			}
		}
		echo JSON ( $result );
	}
	public function media_nearby_media() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$this->load->library ( "media/media" );
		$user_id = $this->get_user_id ();
		$this->load->library ( "common/user_manage" );
		$longitude = $this->input->get ( "longitude" );
		$latitude = $this->input->get ( "latitude" );
		
		$media_list = $this->media->mobile_query_nearby_media ( $user_id, $longitude, $latitude );
		$result ['data'] ['media_list'] = array ();
		if ($media_list) {
			foreach ( $media_list as $media ) {
				$pre = "";
				$status = "AUDIT_PASS";
				$result ['data'] ['media_list'] [] = array (
						"id" => $pre . $media ['media_id'],
						"class" => $media ['media_type'],
						"status" => $status,
						"location" => $media ['location'],
						"region" => array (
								array (
										"id" => $media ['province'],
										"name" => $media ['province_name'] 
								),
								array (
										"id" => $media ['city'],
										"name" => $media ['city_name'] 
								),
								array (
										"id" => $media ['region'],
										"name" => $media ['region_name'] 
								) 
						) 
				);
			}
		}
		echo JSON ( $result );
	}
	private function request_media_id($media_id) {
	}
	public function media_submit_draft() {
		if (is_array ( $this->input->post ( "play_format" ) )) {
			log_message ( "error", "数组形式的播放格式是" . implode ( ",", $this->input->post ( "play_format" ) ) );
		} else {
			log_message ( "error", "字符串的播放格式是" . $this->input->post ( "play_format" ) );
		}
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$this->load->library ( "media/media" );
		$user_id = $this->get_user_id ();
		
		$this->load->library ( "common/user_manage" );
		$user_detail = $this->user_manage->query_user_detail ( $user_id );
		
		$media ['media_type'] = $this->input->post ( "class" );
		$media ['province'] = $this->input->post ( "province" );
		$media ['city'] = $this->input->post ( "city" );
		$media ['region'] = $this->input->post ( "region" );
		$media ['location'] = $this->input->post ( "location" );
		$media ['vendor'] = $this->input->post ( "supplier" );
		$media ['media_type_id'] = $this->input->post ( "media_type" );
		$media ['crafts'] = $this->input->post ( "craftsmanship" );
		$media ['corner_case'] = $this->input->post ( "corner" );
		$media ['illumination'] = $this->input->post ( "illumination" );
		$media ['flow'] = $this->input->post ( "flow" );
		$media ['media_area'] = trim ( $this->input->post ( "media_area" ) );
		$media ['reference_price'] = trim ( $this->input->post ( "reference_price" ) );
		$media ['sketch_map'] = trim ( $this->input->post ( "sketch_map" ) );
		$media ['longitude'] = $this->input->post ( "longitude" );
		$media ['latitude'] = $this->input->post ( "latitude" );
		$media ['specification_width'] = $this->input->post ( "specification_width" );
		$media ['specification_height'] = $this->input->post ( "specification_height" );
		$media ['quote'] = $this->input->post ( "quote" );
		$media ['unique_id'] = $this->input->post ( "unique_id" );
		$media ['media_draft_type'] = $this->input->post ( "media_draft_type" );
		$media ['agreement'] = $this->input->post ( "agreement" );
		$media ['price_remark'] = $this->input->post ( "price_remark" );
		
		$media ['play_time'] = $this->input->post ( "play_time" );
		$media ['play_time_remark'] = $this->input->post ( "play_time_remark" );
		$media ['play_format'] = $this->input->post ( "play_format" );
		$media ['play_frequency'] = $this->input->post ( "play_frequency" );
		$media ['play_frequency_remark'] = $this->input->post ( "play_frequency_remark" );
		$media ['resolution'] = $this->input->post ( "resolution" );
		
		$media ['create_by'] = $user_detail ['user_name'];
		$media ['create_time'] = date ( "Y-m-d H:i:s" );
		$media ['modify_by'] = $user_detail ['user_name'];
		$media ['modify_time'] = date ( "Y-m-d H:i:s" );
		
		$urban_env ['circle'] = $this->input->post ( "circle" );
		$urban_env ['road'] = $this->input->post ( "road" );
		$urban_env ['market'] = $this->input->post ( "market" );
		$urban_env ['station'] = $this->input->post ( "station" );
		$urban_env ['stop'] = $this->input->post ( "stop" );
		$urban_env ['venues'] = $this->input->post ( "venues" );
		$urban_env ['cbd'] = $this->input->post ( "cbd" );
		$urban_env ['chief_district'] = $this->input->post ( "chief_district" );
		$urban_env ['industrial'] = $this->input->post ( "industrial" );
		
		$media ['highway_id'] = $this->input->post ( "highway_id" );
		$media ['section_id'] = $this->input->post ( "section_id" );
		$media ['point_id'] = $this->input->post ( "point_id" );
		
		$media ['airport_id'] = $this->input->post ( "airport" );
		
		$pic = $this->input->post ( "pic" );
		$explanation = $this->input->post ( "explanation" );
		
		$media_draft_id = $this->input->post ( "id" );
		if ($media_draft_id != "" && substr ( $media_draft_id, 0, 2 ) == 'D_') {
			$media_draft_id = substr ( $media_draft_id, 2 );
			$media ['quote'] = "0";
		}
		
		$this->load->library ( "media/media" );
		if ($media_draft_id) {
			$submit_result = $this->media->update_media_draft ( $media, $urban_env, $explanation, $pic, $media_draft_id, 0 );
		} else {
			$submit_result = $this->media->save_media_draft ( $media, $urban_env, $explanation, $pic );
		}
		if ($submit_result ['result'] == false) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 1;
			$result ['msg'] = $submit_result ['message'];
			echo JSON ( $result );
		} else {
			$result ['data'] ['id'] = "D_" . $submit_result ['media_draft_id'];
			echo JSON ( $result );
		}
	}
	public function material_region() {
		$this->load->library ( "sys/region" );
		$region_id = $this->input->get ( 'id' );
		if ($region_id == null) {
			$region_id = 0;
		}
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		if ($region_id == 0) {
			$user_id = $this->get_user_id ();
			$region_list = $this->region->query_user_top_regions ( $user_id );
		} else {
			$region_list = $this->region->get_regions ( $region_id );
		}
		$result ['data'] ['material_list'] = array ();
		if ($region_list) {
			foreach ( $region_list as $region ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $region ['region_id'],
						"name" => $region ['region_name'] 
				);
			}
		}
		
		echo JSON ( $result );
	}
	public function material_supplier() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$region = trim ( $this->input->get ( "province" ) );
		$keyword = urldecode ( $this->input->get ( "keyword" ) );
		if ($region == "") {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 101;
			$result ['msg'] = "缺少参数";
			$result ['data'] = null;
			echo JSON ( $result );
		} else {
			$this->load->library ( "ven/vendor" );
			$vendor_list = $this->vendor->get_vendors ( $region, $keyword );
			$result ['data'] ['material_list'] = array ();
			if ($vendor_list) {
				foreach ( $vendor_list as $vendor ) {
					$result ['data'] ['material_list'] [] = array (
							"id" => $vendor ['vendor_id'],
							"name" => $vendor ['company_name'] 
					);
				}
			}
			echo JSON ( $result );
		}
	}
	public function material_media_type() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$this->load->library ( "sys/media_type" );
		$media_type_list = $this->media_type->get_all_media_type ();
		$result ['data'] ['material_list'] = array ();
		if ($media_type_list) {
			foreach ( $media_type_list as $media_type ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $media_type ['media_type_id'],
						"name" => $media_type ['media_type_name'] 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_craftsmanship() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$this->load->library ( "sys/crafts" );
		$crafts_list = $this->crafts->get_all_crafts ();
		$result ['data'] ['material_list'] = array ();
		if ($crafts_list) {
			foreach ( $crafts_list as $crafts ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $crafts ['crafts_id'],
						"name" => $crafts ['crafts_name'] 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_corner() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$this->load->library ( "sys/corner_case" );
		$corner_case_list = $this->corner_case->get_all_corner_case ();
		$result ['data'] ['material_list'] = array ();
		if ($corner_case_list) {
			foreach ( $corner_case_list as $corner_case ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $corner_case ['corner_case_id'],
						"name" => $corner_case ['corner_case_name'] 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_media_area() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$this->load->library ( "sys/media_area" );
		$media_area_list = $this->media_area->get_all_media_area ();
		$result ['data'] ['material_list'] = array ();
		if ($media_area_list) {
			foreach ( $media_area_list as $media_area ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $media_area ['media_area_id'],
						"name" => $media_area ['media_area_name'] 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_traffic_flow() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'] = array (
				array (
						"id" => "10",
						"name" => "极大" 
				),
				array (
						"id" => "20",
						"name" => "较大" 
				),
				array (
						"id" => "30",
						"name" => "一般" 
				),
				array (
						"id" => "40",
						"name" => "较小" 
				) 
		);
		echo JSON ( $result );
	}
	public function material_highway() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$keyword = urldecode ( $this->input->get ( "keyword" ) );
		$this->load->library ( "env/env_highway" );
		$highway_list = $this->env_highway->get_all_highway ( $keyword );
		$result ['data'] ['material_list'] = array ();
		
		if ($highway_list) {
			foreach ( $highway_list as $highway ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $highway ['highway_id'],
						"name" => $highway ['highway_name'] 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_highway_section() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$highway_id = $this->input->get ( "highway" );
		$this->load->library ( "env/highway_section" );
		$section_list = $this->highway_section->get_all_section ( $highway_id );
		$result ['data'] ['material_list'] = array ();
		if ($section_list) {
			foreach ( $section_list as $section ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $section ['section_id'],
						"name" => $section ['section_name'] 
				);
			}
		}
		
		echo JSON ( $result );
	}
	public function material_highway_point() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$section_id = $this->input->get ( "highway_section" );
		$this->load->library ( "env/highway_point" );
		$point_list = $this->highway_point->get_all_point ( $section_id );
		$result ['data'] ['material_list'] = array ();
		if ($point_list) {
			foreach ( $point_list as $point ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $point ['point_id'],
						"name" => $point ['point_name'] 
				);
			}
		}
		
		echo JSON ( $result );
	}
	public function material_airport() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$keyword = urldecode ( $this->input->get ( "keyword" ) );
		$data ['media_type'] = "airport";
		$this->load->library ( "env/env_airport" );
		$airport_list = $this->env_airport->get_all_airport ( $keyword );
		
		$result ['data'] ['material_list'] = array ();
		
		if ($airport_list) {
			foreach ( $airport_list as $airport ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $airport ['airport_id'],
						"name" => $airport ['airport_name'] 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_business_area() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'] = $this->get_urban_env_list ( "circle" );
		echo JSON ( $result );
	}
	public function material_main_road() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'] = $this->get_urban_env_list ( "road" );
		echo JSON ( $result );
	}
	public function material_market() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'] = $this->get_urban_env_list ( "market" );
		echo JSON ( $result );
	}
	public function material_railway_station() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'] = $this->get_urban_env_list ( "station" );
		echo JSON ( $result );
	}
	public function material_bus_station() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'] = $this->get_urban_env_list ( "stop" );
		echo JSON ( $result );
	}
	public function material_major_venues() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'] = $this->get_urban_env_list ( "venues" );
		echo JSON ( $result );
	}
	public function material_brand() {
		$this->load->library ( "sys/brand" );
		$keyword = $this->input->get ( 'keyword' );
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$brand_list = $this->brand->search_brand ( $keyword );
		$result ['data'] ['material_list'] = array ();
		if ($brand_list) {
			$result ['data'] ['material_list'] = $brand_list;
		}
		echo JSON ( $result );
	}
	public function material_industry() {
		$this->load->library ( "sys/industry" );
		$industry_id = $this->input->get ( 'id' );
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		if ($industry_id == null) {
			$industry_id = 0;
		}
		
		$industry_list = $this->industry->get_industrys ( $industry_id );
		
		$result ['data'] ['material_list'] = array ();
		if ($industry_list) {
			foreach ( $industry_list as $industry ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $industry ['industry_id'],
						"name" => $industry ['industry_name'] 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_resolution() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'];
		if ($this->config->item ( "resolutions" ) != null) {
			foreach ( $this->config->item ( "resolutions" ) as $resolution ) {
				$result ['data'] ['material_list'] [] = array (
						"id" => $resolution,
						"name" => $resolution 
				);
			}
		}
		echo JSON ( $result );
	}
	public function material_agreement() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'];
		if ($this->config->item ( "agreement" )) {
			$result ['data'] ['material_list'] = $this->config->item ( "agreement" );
		}
		echo JSON ( $result );
	}
	public function material_play_format() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		$result ['data'] ['material_list'];
		if ($this->config->item ( "play_format" )) {
			$result ['data'] ['material_list'] = $this->config->item ( "play_format" );
		}
		echo JSON ( $result );
	}
	public function supervise_submit_monitor() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = array ();
		
		$monitor ['media_id'] = $this->input->get_post ( "media_id" );
		$monitor ['industry'] = $this->input->get_post ( "industry" );
		$monitor ['industry_category'] = $this->input->get_post ( "industry_category" );
		$monitor ['product_name'] = $this->input->get_post ( "product_name" );
		$monitor ['brand'] = $this->input->get_post ( "brand" );
		
		$user_id = $this->get_user_id ();
		$this->load->library ( "common/user_manage" );
		$user_detail = $this->user_manage->query_user_detail ( $user_id );
		
		$monitor ['monitor_by'] = $user_detail ['user_name'];
		$monitor ['monitor_time'] = date ( "Y-m-d H:i:s" );
		$this->load->library ( "media/monitor" );
		$submit_result = $this->monitor->save_monitor ( $monitor );
		
		if ($submit_result ['result'] == false) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 1;
			$result ['msg'] = $submit_result ['message'];
			echo JSON ( $result );
		} else {
			$result ['data'] ['id'] = $submit_result ['monitor_id'];
			echo JSON ( $result );
		}
	}
	public function supervise_delete_monitor() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$this->load->library ( "media/media" );
		$monitor_id = $this->input->post ( 'id' );
		
		$this->load->library ( "media/monitor" );
		$this->monitor->delete_monitor ( $monitor_id );
		echo JSON ( $result );
	}
	public function supervise_update_monitor() {
		$this->load->library ( "sys/industry" );
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$monitor ['media_id'] = $this->input->get_post ( "media_id" );
		$monitor ['industry'] = $this->input->get_post ( "industry" );
		$monitor ['industry_category'] = $this->input->get_post ( "industry_category" );
		$monitor ['product_name'] = $this->input->get_post ( "product_name" );
		$monitor ['brand'] = $this->input->get_post ( "brand" );
		$monitor_id = $this->input->get_post ( "id" );
		if ($monitor_id == "") {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 1;
			$result ['msg'] = "没有ID";
			echo JSON ( $result );die();
		}
		log_message ( "info", "产品名称->" . $monitor ['product_name'] );
		log_message ( "info", "brand->" . $monitor ['brand'] );
		log_message ( "info", "id->" . $monitor_id );
		$user_id = $this->get_user_id ();
		$this->load->library ( "common/user_manage" );
		$user_detail = $this->user_manage->query_user_detail ( $user_id );
		
		$monitor ['monitor_by'] = $user_detail ['user_name'];
		$monitor ['monitor_time'] = date ( "Y-m-d H:i:s" );
		$this->load->library ( "media/monitor" );
		$submit_result = $this->monitor->update_monitor ( $monitor, $monitor_id );
		
		if ($submit_result ['result'] == false) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 1;
			$result ['msg'] = $submit_result ['message'];
			echo JSON ( $result );
		} else {
			echo JSON ( $result );
		}
	}
	public function supervise_get_monitor() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$monitor_id = $this->input->get ( "id" );
		
		$this->load->library ( "media/monitor" );
		$monitor_detail = $this->monitor->query_monitor_detail ( $monitor_id );
		
		if ($monitor_detail == null) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 101;
			$result ['msg'] = "监测信息不存在";
			$result ['data'] = null;
		} else {
			$result ['data'] = array (
					"id" => $monitor_id,
					"media_id" => $monitor_detail ['media_id'],
					"industry" => array (
							array (
									"id" => $monitor_detail ['industry'],
									"name" => $monitor_detail ['industry_name'] 
							),
							array (
									"id" => $monitor_detail ['industry_category'],
									"name" => $monitor_detail ['industry_category_name'] 
							) 
					),
					"product_name" => $monitor_detail ['product_name'],
					"brand" => array (
							"id" => $monitor_detail ['brand'],
							"name" => $monitor_detail ['brand_name'] 
					),
					"monitor_pic" => $monitor_detail ['monitor_pic'] == "" ? "" : base_url () . "acc_files_path" . $monitor_detail ['monitor_pic'],
					"monitor_by" => $monitor_detail ['monitor_by'],
					"monitor_time" => substr ( $monitor_detail ['monitor_time'], 0, 16 ) 
			);
		}
		echo JSON ( $result );
	}
	public function supervise_get_list() {
		$this->load->library ( "sys/industry" );
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		$result ['data'] = null;
		
		$user_id = $this->get_user_id ();
		
		$search ['province'] = $this->input->get ( "province" );
		$search ['city'] = $this->input->get ( "city" );
		$search ['region'] = $this->input->get ( "region" );
		$search ['industry'] = $this->input->get ( "industry" );
		$search ['industry_category'] = $this->input->get ( "industry_category" );
		$search ['product_name'] = $this->input->get ( "product_name" );
		$search ['monitor_time'] = $this->input->get ( "monitor_time" );
		$search ['media_type'] = $this->input->get ( "media_type" );
		$search ['media_type_id'] = $this->input->get ( "media_type_id" );
		$search ['brand'] = $this->input->get ( "brand" );
		$search ['dianwei'] = $this->input->get ( "dianwei" );
		$search ['brand_name'] = "";
		
		$this->load->library ( "media/monitor" );
		$result ['data'] ['monitor_list'] = array ();
		$monitor_list = $this->monitor->query_monitor_list ( $user_id, $search );
		if ($monitor_list) {
			foreach ( $monitor_list as $monitor ) {
				$result ['data'] ['monitor_list'] [] = array (
						"id" => $monitor ['monitor_id'],
						"industry" => array (
								array (
										"id" => $monitor ['industry'],
										"name" => $monitor ['industry_name'] 
								),
								array (
										"id" => $monitor ['industry_category'],
										"name" => $monitor ['industry_category_name'] 
								) 
						),
						"media_id" => $monitor ['media_id'],
						"product_name" => $monitor ['product_name'],
						"monitor_time" => substr ( $monitor ['monitor_time'], 0, 10 ) 
				);
			}
		}
		echo JSON ( $result );
	}
	public function supervise_upload_pic() {
		$result ['ret_code'] = 0;
		$result ['error_code'] = 0;
		$result ['msg'] = "";
		
		$this->load->library ( "media/monitor" );
		$monitor_id = $this->input->post ( "id" );
		
		$upload_result = $this->monitor->save_monitor_pic ( "pic" );
		if ($upload_result ['result'] == false) {
			$result ['ret_code'] = 1;
			$result ['error_code'] = 1;
			$result ['msg'] = $upload_result ['message'];
		} else {
			$result ['data'] ['upload_file'] = $upload_result ['upload_file'];
			$monitor ['monitor_pic'] = $result ['data'] ['upload_file'];
			$this->monitor->modify_monitor_pic ( $monitor, $monitor_id );
		}
		echo JSON ( $result );
	}
	private function get_urban_env_list($type_name) {
		$region = $this->input->get ( "area" );
		$list = array ();
		if ($type_name == "road") {
			$this->load->library ( "env/urban_road" );
			$env_list = $this->urban_road->get_env_list ( $region );
		} else {
			$this->load->library ( "env/urban_circle" );
			$env_list = $this->urban_circle->get_env_list ( $type_name, $region );
		}
		if ($env_list) {
			foreach ( $env_list as $env ) {
				$list [] = array (
						"id" => $env ['env_id'],
						"name" => $env ['env_name'] 
				);
			}
		}
		return $list;
	}
}